package com.gs.cd.service.resource.mapper;

import com.gs.cd.model.resource.SysResource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author seven
 * @since 2020-12-22
 */
public interface SysResourceMapper extends BaseMapper<SysResource> {

    /**
     * 非mybatis id自增的添加
     *
     * @param sysResourceList
     * @return
     */
    @Insert("<script>" +
            "insert into sys_resource(\"id\", \"parent_id\", \"name\", \"perms\", \"front_url\", \"back_url\", \"resource_type\", \"sort\", \"creator\", \"create_time\", \"updator\", \"update_time\"" +
            ", \"icon_class\", \"icon_color\", \"category\", \"product_id\") values" +
            "<foreach collection='sysResourceList' item='sysResource' separator=','>" +
            "(#{sysResource.id},#{sysResource.parentId},#{sysResource.name},#{sysResource.perms}," +
            "#{sysResource.frontUrl},#{sysResource.backUrl},#{sysResource.resourceType},#{sysResource.sort}," +
            "#{sysResource.creator},#{sysResource.createTime},#{sysResource.updator},#{sysResource.updateTime}," +
            "#{sysResource.iconClass},#{sysResource.iconColor},#{sysResource.category},#{sysResource.productId})" +
            "</foreach>" +
            "</script>")
    int insertBatchNotAuto(@Param("sysResourceList") Collection<SysResource> sysResourceList);

    /**
     * 获取资源 关联信息
     *
     * @param id
     * @return
     */
    @Select("SELECT c.name as group_name ,d.sale_name FROM sys_resource a\n" +
            "LEFT JOIN sys_resource_group_own_resource b\n" +
            "ON a.perms = b.sys_resource_perms\n" +
            "LEFT JOIN sys_resource_group c\n" +
            "ON c.code = b.sys_resource_group_code\n" +
            "and b.sys_sale_code = c.sale_code\n" +
            "LEFT JOIN sys_sale d\n" +
            "ON d.code = b.sys_sale_code\n" +
            "WHERE a.id = #{id}")
    public List<Map<String, String>> getRelatedInfo(@Param("id") String id);
}
